﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Web.UI.WebControls;
using System.Data;

namespace VIT.Web.Admin
{
    public partial class LinkList : VITComponent
    {
        Provider.Link obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            obj = new Provider.Link();

            if (!Page.IsPostBack)
            {
                loadLang();
            }
        }

        #region
        [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
        public DataTable GetData(string SortColumn)
        {
            Provider.Link l = new Provider.Link();
            l.LanguageId = Session["language"].ToString();
            return l.GetAll(SortColumn);            
        }

        private void loadLang()
        {
            ddlLang.DataSource = new Language().getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString() ; 
        }

        public void deleteList(string[] listId)
        {
            foreach (string id in listId)
                obj.Delete(1, int.Parse(id));
        }

        public void PublishList(int pub, string[] listId)
        {
            foreach (string id in listId)
                obj.Publish(pub, int.Parse(id));
        }
        #endregion

        #region Events
        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
            ListView.DataBind();
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            RedirectComponent("Link", "LinkEdit");
        }

        protected void Published_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(1, check);
            ListView.DataBind();
        }

        protected void UnPublished_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            PublishList(0, check);
            ListView.DataBind();
        }
        
        protected void changePublish_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            int pub = (id[1] == "True") ? 0 : 1;
            obj.Publish(pub, int.Parse(id[0]));
            ListView.DataBind();
        }

        protected void updateOrder_Click(object sender, EventArgs e)
        {
            LinkSubmit btn = (LinkSubmit)sender;
            string check = btn.Param.ToString();

            string[] id = check.Split('|');
            obj.Order(id[1], int.Parse(id[0]));
            ListView.DataBind();
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
        }

        protected void Pager_PreRender(object sender, EventArgs e)
        {
            ListView.DataBind();
        }
        #endregion
    }
}
